package com.bearbig.spring.cloud.example.services;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * <p></p>
 *
 * @author xiongyu
 * @version $Id: com.bearbig.spring.cloud.example.services 2017/1/28 08:08 xiongyu $
 */
@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    // 指定调用失败后的回调方法：fallMethod
    @HystrixCommand(fallbackMethod = "fallMethod")
    public String getUserNameById(Integer id){
        return restTemplate.getForEntity("http://user-service/findUserById?id="+id, String.class).getBody();
    }

    // 此回调方法必须跟调用此方法的函数参数类型保持一致，否则会抛出异常
    public String fallMethod(Integer id){
        return "error";
    }
}
